<?php
require_once __DIR__ . '/limonade/lib/limonade.php';
require_once __DIR__ . '/options.php';

//
// Here I'm just extending Limonade a little bit with the ability to render a 
// specific `view` file if the `langyage` option is set
//
function custom_render( $file_name, $layout = '', $locals = array() )
{
	// if we don't have the layout we set it
	if( ! $layout )
	{
		$layout = layout();
	}

	// we check if we have the `language` option 
	// and try to find a corresponding view
	if( option( 'language' ) )
	{
		$prefix = option( 'language' ) . '_';
		$file = option( 'views_dir' ) . '/' . $prefix . $file_name;

		if( file_exists( $file ) )
		{
			return render( $prefix . $file_name, $layout, $locals );
		}
	}

	return render( $file_name, $layout, $locals );
}

//
// this will run before everything ...
//
function before()
{
	layout( 'layout.html.php' );

	if( option( 'language' ) )
	{
		$prefix = option( 'language' ) . '_';
		// TODO this is pretty lame, but good for now ...
		$file = option( 'views_dir' ) . '/' . $prefix . 'layout.html.php';
		if( file_exists( $file ) )
		{
			layout( $prefix . 'layout.html.php' );
		}
	}
}

dispatch('/', 'root');
    function root()
    {
        return custom_render( 'home.html.php' );
    }

dispatch( '/calendar', 'calendar' );
	function calendar()
	{
		return custom_render( 'calendar.html.php' );
	}

dispatch( '/hello/:name', 'hello' );
	function hello()
	{
		$name = params( 'name' );
		return render( "Hello $name" );
	}
	
run();
